package com.qf.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
@WebServlet("/show/req")
public class Demo1Servlet extends HttpServlet {

/**
     * 通过HttpServletRequest req对象,完成接收请求中的各种数据*/
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 请求uri: 资源标识符
        String uri = req.getRequestURI( );
        System.out.println("uri = " + uri);

        // url: 资源定位符
        StringBuffer url = req.getRequestURL( );
        System.out.println("url = " + url);

        // 获得请求的端口
        int remotePort = req.getRemotePort( );
        System.out.println("remotePort = " + remotePort);

        // 获得请求方法
        String method = req.getMethod( );
        System.out.println("method = " + method);

        System.out.println("------ 请求头(开始) ------");
        // 获得请求头[了解]
        String accept = req.getHeader("accept");
        System.out.println("accept = " + accept);

        Enumeration<String> headerNames = req.getHeaderNames( );
        while (headerNames.hasMoreElements( )) {
            String head = headerNames.nextElement( );
            String value = req.getHeader(head);
            System.out.println(head+" : " + value );

        }
        System.out.println("------ 请求头(结束) ------");
        System.out.println("------ 请求数据(开始) 非常重要 ------");
        // 无论是明文,暗文,单选,日期,下拉都是一个方法
        // 参数是前端输入框的name值
        String username = req.getParameter("username");
        System.out.println("username = " + username);

        String password = req.getParameter("password");
        System.out.println("password = " + password);

        String sex = req.getParameter("sex");
        System.out.println("sex = " + sex);
        // 将来存储数字,所以解析为数字
        int sexNum = Integer.parseInt(sex);

        String email = req.getParameter("email");
        System.out.println("email = " + email);

        String birthday = req.getParameter("birthday");// 2024-01-01
        System.out.println("birthday = " + birthday);
        // 将来存储日期,所以解析为日期对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = sdf.parse(birthday);
        } catch (ParseException e) {
            System.out.println("日期解析失败" );
        }

        String edu = req.getParameter("edu");
        System.out.println("edu = " + edu);

        String beizhu = req.getParameter("beizhu");
        System.out.println("beizhu = " + beizhu);

        // 复选框,需要使用数组接收
        String[] skills = req.getParameterValues("skill");
        for (int i = 0; i < skills.length; i++) {
            System.out.println(skills[i] );
        }
        System.out.println("------ 请求数据(结束) ------");

        // 没有响应
    }
}
